package cn.xiaoxiang.org.controller;

import cn.xiaoxiang.basic.util.LoginContext;
import cn.xiaoxiang.org.dto.ShopAuditDto;
import cn.xiaoxiang.org.dto.ShopSettlementDto;
import cn.xiaoxiang.org.query.ShopQuery;
import cn.xiaoxiang.org.service.IShopService;
import cn.xiaoxiang.org.domain.Shop;

import cn.xiaoxiang.basic.util.AjaxResult;
import cn.xiaoxiang.basic.util.PageList;

import cn.xiaoxiang.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("保存数据失败"+e.getMessage());

        }
    }
    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }


    /**
     * 查看所有的员工信息
     * @return
     */
    @GetMapping()
    public List<Shop> list(){

        return shopService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query){
//        Logininfo loginUser = LoginContext.getLoginUser(request);
//        System.out.println(loginUser);
        return shopService.queryByPage(query);
    }

    //   首次审核 settlement
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid ShopSettlementDto vo)
    {
        shopService.settlement(vo);
        return AjaxResult.me().success();
    }
    //第二次
    @PostMapping("/settlement/again")
    public AjaxResult settlementagain(@RequestBody @Valid Shop shop)
    {
        shopService.settlementagain(shop);
        return AjaxResult.me().success();
    }

//审核通过
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody @Valid ShopAuditDto dto)
    {
        shopService.auditPass(dto);
        return AjaxResult.me().success();
    }//驳回
    @PostMapping("audit/reject")
    public AjaxResult auditReject(@RequestBody @Valid ShopAuditDto dto)
    {
        shopService.auditReject(dto);
        return AjaxResult.me().success();
    }
    //激活
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id)
    {
        shopService.active(id);
        return AjaxResult.me().success();
    }

}
